<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item label="订单号">
          <el-input v-model="listQuery.orderNo" placeholder="订单号"></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="listQuery.status" placeholder="订单状态">
            <el-option
              v-for="item in orderStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter">查询</el-button>
          <el-button type="primary" @click="handleBatchDeliver" :disabled="!selectedOrders.length">
            批量发货
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="orderNo" label="订单号" width="180"></el-table-column>
      <el-table-column prop="userName" label="用户名" width="120"></el-table-column>
      <el-table-column prop="totalAmount" label="订单金额" width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.totalAmount.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="订单状态" width="120">
        <template slot-scope="scope">
          <el-tag :type="orderStatusTypeMap[scope.row.status]">
            {{ orderStatusMap[scope.row.status] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.status === 20"
            @click="handleDeliver(scope.row)">发货</el-button>
          <el-button
            size="mini"
            type="warning"
            v-if="scope.row.status === 40"
            @click="handleRefund(scope.row)">退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 订单详情对话框 -->
    <el-dialog title="订单详情" :visible.sync="detailDialogVisible" width="50%">
      <div v-if="currentOrder">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="订单号">{{ currentOrder.orderNo }}</el-descriptions-item>
          <el-descriptions-item label="订单状态">
            {{ orderStatusMap[currentOrder.status] }}
          </el-descriptions-item>
          <el-descriptions-item label="收货人">{{ currentOrder.receiverName }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ currentOrder.receiverPhone }}</el-descriptions-item>
          <el-descriptions-item label="收货地址" :span="2">
            {{ currentOrder.receiverAddress }}
          </el-descriptions-item>
        </el-descriptions>

        <el-table
          :data="currentOrder.items"
          style="width: 100%; margin-top: 20px;">
          <el-table-column prop="productName" label="商品名称"></el-table-column>
          <el-table-column prop="price" label="单价">
            <template slot-scope="scope">
              ¥{{ scope.row.price.toFixed(2) }}
            </template>
          </el-table-column>
          <el-table-column prop="quantity" label="数量"></el-table-column>
          <el-table-column prop="subtotal" label="小计">
            <template slot-scope="scope">
              ¥{{ (scope.row.price * scope.row.quantity).toFixed(2) }}
            </template>
          </el-table-column>
        </el-table>

        <div class="order-total">
          总计：¥{{ currentOrder.totalAmount.toFixed(2) }}
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrderList, deliverOrder, refundOrder } from '@/api/order'
import Pagination from '@/components/Pagination'

export default {
  name: 'OrderList',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      selectedOrders: [],
      listQuery: {
        page: 1,
        limit: 10,
        orderNo: '',
        status: ''
      },
      orderStatusOptions: [
        { value: 10, label: '待付款' },
        { value: 20, label: '待发货' },
        { value: 30, label: '已发货' },
        { value: 40, label: '已完成' },
        { value: 50, label: '已取消' },
        { value: 60, label: '退款中' },
        { value: 70, label: '已退款' }
      ],
      orderStatusMap: {
        10: '待付款',
        20: '待发货',
        30: '已发货',
        40: '已完成',
        50: '已取消',
        60: '退款中',
        70: '已退款'
      },
      orderStatusTypeMap: {
        10: 'warning',
        20: 'primary',
        30: 'success',
        40: 'success',
        50: 'info',
        60: 'danger',
        70: 'info'
      },
      detailDialogVisible: false,
      currentOrder: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getOrderList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSelectionChange(val) {
      this.selectedOrders = val
    },
    handleDetail(row) {
      this.currentOrder = row
      this.detailDialogVisible = true
    },
    handleDeliver(row) {
      this.$confirm('确认发货？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deliverOrder(row.id).then(() => {
          this.$message.success('发货成功')
          this.getList()
        })
      })
    },
    handleBatchDeliver() {
      const ids = this.selectedOrders.map(item => item.id)
      this.$confirm(`确认发货选中的 ${ids.length} 个订单？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用批量发货API
        this.$message.success('批量发货成功')
        this.getList()
      })
    },
    handleRefund(row) {
      this.$confirm('确认退款？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        refundOrder(row.id).then(() => {
          this.$message.success('退款成功')
          this.getList()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.order-total {
  text-align: right;
  margin-top: 20px;
  font-size: 16px;
  font-weight: bold;
}
</style> 